package com.ruihu.api.module.business.task.controller;

import com.ruihu.admin.common.anno.NoValidPrivilege;
import com.ruihu.admin.common.anno.OperateLog;
import com.ruihu.admin.common.controller.BaseController;
import com.ruihu.admin.common.domain.PageResultDTO;
import com.ruihu.admin.common.domain.ResponseDTO;
import com.ruihu.admin.constant.SwaggerTagConst;
import com.ruihu.admin.module.business.task.constant.TaskStatusEnum;
import com.ruihu.admin.module.business.task.domain.dto.SubTaskQueryDTO;
import com.ruihu.admin.module.business.task.domain.dto.TaskNodeInstQueryDTO;
import com.ruihu.admin.module.business.task.domain.dto.TaskQueryDTO;
import com.ruihu.admin.module.business.task.domain.vo.SubTaskVO;
import com.ruihu.admin.module.business.task.domain.vo.TaskNodeInstVO;
import com.ruihu.admin.module.business.task.domain.vo.TaskVO;
import com.ruihu.admin.module.business.task.service.SubTaskService;
import com.ruihu.admin.module.business.task.service.TaskNodeInstService;
import com.ruihu.admin.module.business.task.service.TaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController(value = "taskApiController")
@Api(tags = {SwaggerTagConst.Mobile.TASK_API})
@OperateLog
public class TaskController extends BaseController {

    @Autowired
    private TaskService taskService;

    @Autowired
    private SubTaskService subTaskService;

    @Autowired
    private TaskNodeInstService taskNodeInstService;


    @ApiOperation(value = "分页查询主任务",notes = "@author Tony")
    @PostMapping("/api/task/page/query")
    @NoValidPrivilege
    public ResponseDTO<PageResultDTO<TaskVO>> queryByPage(@RequestBody @Validated TaskQueryDTO queryDTO) {
        queryDTO.setStatus(TaskStatusEnum.RUNNING.getValue());
        return taskService.queryByPage(queryDTO);
    }

    @ApiOperation(value = "分页查询子任务",notes = "@author Tony")
    @PostMapping("/api/subtask/page/query")
    @NoValidPrivilege
    public ResponseDTO<PageResultDTO<SubTaskVO>> querySubTaskByPage(@RequestBody @Validated SubTaskQueryDTO queryDTO) {
        return this.subTaskService.querySelfAndEmptySubTask(queryDTO);
    }

    @ApiOperation(value = "根据ID查询子任务",notes = "@author Tony")
    @PostMapping("/api/subTask/{subTaskId}")
    @NoValidPrivilege
    public ResponseDTO<SubTaskVO> querySubTask(@PathVariable("subTaskId")  Long subTaskId) {
        return this.subTaskService.queryById(subTaskId);
    }

    @ApiOperation(value = "查询任务节点",notes = "@author Tony")
    @PostMapping("/api/task/{taskId}/node/query")
    @NoValidPrivilege
    public ResponseDTO<List<TaskNodeInstVO>> queryTaskNodeList(@PathVariable("taskId")  Long taskId) {
        TaskNodeInstQueryDTO queryDTO = new TaskNodeInstQueryDTO();
        queryDTO.setMainTaskId(taskId);
        return this.taskNodeInstService.queryByList(queryDTO);
    }

    @ApiOperation(value = "执行子任务",notes = "@author Tony")
    @PostMapping("/api/task/{taskId}/subtask/{subTaskId}/execute")
    @NoValidPrivilege
    public ResponseDTO<List<TaskNodeInstVO>> executeSubTask(@PathVariable("taskId") Long taskId,
                                                            @PathVariable("subTaskId") Long subTaskId) {
        return this.subTaskService.executeSubTask(taskId, subTaskId);
    }

    @ApiOperation(value = "清空子任务", notes = "清空子任务")
    @PostMapping("/api/task/{subTaskId}/reset")
    @NoValidPrivilege
    public ResponseDTO<String> resetSubTask(@PathVariable("subTaskId") Long subTaskId) {
        return this.subTaskService.resetSubTask(subTaskId);
    }

}
